import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;

@SuppressWarnings("serial")
public class MapView extends JPanel implements MouseListener, MouseMotionListener {
	//map properties-----------------------------------------
	private Image map, dMap;
	private MapDatabaseEntry properties;
	//-------------------------------------------------------
	
	private Point getMerc(double lon, double lat){
		double x, y, s;
		
		x = lon * properties.h();
		
		s = Math.sin(Math.toRadians(lat));
		y = (Math.log((1 + s)/(1 - s)) / 2);
		y = (y / Math.PI) * (dMap.getHeight(null)/2)/85;
		
		return new Point((int)x,(int)-y);
	}
	
    private double  mercX(double lon) {
    	return Math.toRadians(lon) * properties.a();
    }
 
    private double mercY(double lat) {
        if (lat > 89.5)  lat = 89.5;
        if (lat < -89.5) lat = -89.5;
        
        double rlat = Math.toRadians(lat);
        double sinlat = Math.sin(rlat);
        
        double y = (1 + sinlat)/(1 - sinlat);
        y = Math.log(y);
        y = properties.a()/2 * y;
        
        return -y;
    }

	
	public MapView(MapDatabaseEntry m) {
	    setMinimumSize(new Dimension(200, 200));
	    setSize(500,500);
	    setBackground(Color.red);
	    
		properties = m;
		dMap = map = Toolkit.getDefaultToolkit().createImage(m.name());
        while ((map.getWidth(null) == -1) && (map.getHeight(null) == -1));        
    	
		addMouseListener(this);
		addMouseMotionListener(this);
        
	    repaint();
	}
	
	private void drawPoint(Graphics aPen, int l, int t, Coord i) {
		Point p = getMerc(i.getLongitude(), i.getLatitude());
		aPen.setColor(Color.CYAN);
		aPen.fillOval(l+p.x-5, t+p.y-5, 10, 10);
	}
	
    public void paintComponent(Graphics aPen) {
        	super.paintComponent(aPen);

        	//displaced screen center
        	int cx = (getWidth()/2) - offset.x - offsetTemp.x,
        		cy = (getHeight()/2) - offset.y - offsetTemp.y;
        	
        	//displaced screen top left
        	int l = cx - properties.pA().x,
        		t = cy - properties.pA().y;
        	
        	aPen.drawImage(dMap, l, t, null);
        	
        	drawPoint(aPen,cx,cy, new Coord(0,0));
        	drawPoint(aPen,cx,cy, new Coord(15,15));
	}
    
    //CLICK EVENTS FOR MAP NAVIGATION
	//Inherited Events
    private Point clickOn, clickOff;
    
	private Point offset = new Point(0,0); //for scrolling
	private Point offsetTemp = new Point(0,0);
	
	public void mousePressed(MouseEvent event) {
		clickOn = event.getPoint();
	}
	public void mouseDragged(MouseEvent event) {
		clickOff = event.getPoint();
		
		offsetTemp.x = clickOn.x - clickOff.x;
		offsetTemp.y = clickOn.y - clickOff.y;
		
		repaint();
	}
	
    public void mouseReleased(MouseEvent event) {
    	offset.x += offsetTemp.x;
    	offset.y += offsetTemp.y;
    	offsetTemp = new Point(0,0);
    	repaint();
    }
    
	public void mouseClicked(MouseEvent event) {}
    public void mouseEntered(MouseEvent event) {}
	public void mouseExited(MouseEvent event) {}
	public void keyTyped(KeyEvent event) {}
	public void keyReleased(KeyEvent event) {}
    public void mouseMoved(MouseEvent event) { }
}
